home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 991 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  39 lines

  1. Path: monet.ccs.itd.umich.edu!dogcow
  2. From: dogcow@monet.ccs.itd.umich.edu (Tom Spindler)
  3. Newsgroups: comp.lang.c
  4. Subject: Extracting chars from shorts.
  5. Date: 10 Jan 1996 19:20:32 GMT
  6. Organization: University of Michigan
  7. Message-ID: <4d13i0$k3s@lastactionhero.rs.itd.umich.edu>
  8. NNTP-Posting-Host: monet.ccs.itd.umich.edu
  9.  
  10. I'm trying to extracts individual bytes from a double. The following code
  11. doesn't work:
  12.  
  13. typedef unsigned char zbyte;  /* unsigned 1 byte quantity */
  14. typedef unsigned short zword; /* unsigned 2 byte quantity */
  15.  
  16. typedef union zw_u_t {
  17.         zword zword;
  18.         struct {zbyte hi,lo;} zbyte;
  19.         } zwordun;
  20.  
  21. #define lo(v)      ((zbyte) (((zwordun) v).zbyte.lo))
  22. #define hi(v)      ((zbyte) (((zwordun) v).zbyte.hi))
  23.  
  24. main() {
  25. zword foon;
  26. zbyte zfoo;
  27.  
  28. zfoo = lo(foon);
  29. }
  30.  
  31. One compiler complains that it's an illegal cast, whereas gcc says that the
  32. return value is an int.
  33.  
  34. I could do something like ((char *)&v[0]), but then it would crash if I
  35. tried to do something with lo(42).
  36.  
  37. Any suggestions? (I already know what endian the code is supposed to run on.)
  38.  
  39.